package org.hackystat.ambientdevice.trigger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.hackystat.ambientdevice.server.jaxb.Project;
import org.hackystat.ambientdevice.server.jaxb.Trigger;
import org.hackystat.sensorbase.client.SensorBaseClient;
import org.hackystat.sensorbase.client.SensorBaseClientException;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef;
import org.hackystat.utilities.tstamp.Tstamp;

/**
 * I don't know what this does shao never left comments.
 * 
 * @author Shaoxuan Zhang
 * @author Sonwright M. Gomez
 */
public class FileConflictDetector extends SensorDataTrigger {

  /**
   * default serial.
   */
  private static final long serialVersionUID = 1L;

  /**
   * 
   * @param project the project
   * @param trigger the trigger for the project
   * @param client the client for the project
   */
  public FileConflictDetector(Project project, Trigger trigger, SensorBaseClient client) {
    super(project, trigger, client);
    this.setId("File Conflict Detector");
  }
  
  /**
   * gets the state.
   * @return whether it works or not
   */
  public boolean getState() {
    XMLGregorianCalendar currentTime = Tstamp.makeTimestamp();
    if (this.getLastUpdateTime() == null) {
      System.out.println(this.getId() + 
          ": first time running, searching back 3 days for initializing data.");
      this.setLastUpdateTime(Tstamp.incrementDays(currentTime, -3));
    }
    List<SensorData> dataList = new ArrayList<SensorData>();
    try {
      SensorDataIndex index = 
        getClient().getProjectSensorData(getProject().getOwner(), getProject().getProjectName(),
          getLastUpdateTime(), currentTime, "DevEvent");
      if (index.getSensorDataRef().size() <= 0) {
        return false;
      }
      List<SensorDataRef> sensorDataRefList = index.getSensorDataRef();
      Collections.sort(sensorDataRefList, new CompareSensorDataRefByDate());
      for (SensorDataRef reference : sensorDataRefList) {
        dataList.add(getClient().getSensorData(reference));
      }
    }
    catch (SensorBaseClientException e) {
      System.out.println(this.getId() + e.getMessage());
      return false;
    }
    /*
     * Example Properties:
     * 
     * Subtype=Open
     * Unit-Type=file
     * Unit-Name=CommitTrigger.java
     */
    for (SensorData data : dataList) {
      getPropertyValue(data, "Type");
      getPropertyValue(data, "Subtype");
      getPropertyValue(data, "Unit-Name");
    }
    
    return false;
  }
  /**
   * Gets the value for the given property name from the <code>Properties</code> object contained
   * in the given sensor data instance.
   * 
   * @param data The sensor data instance to get the property from.
   * @param propertyName The name of the property to get the value for.
   * @return Returns the value of the property or null if no matching property was found.
   */
  public static String getPropertyValue(SensorData data, String propertyName) {
    Properties properties = data.getProperties();
    if (properties != null) {
      List<Property> propertyList = properties.getProperty();
      for (Property property : propertyList) {
        if (property.getKey().equals(propertyName)) {
          return property.getValue();
        }
      }
    }
    return null;
  }
}
